#include "stm32f10x.h"                  // Device header
#include "Key.h"
#include "delay.h"

struct 
{
	uint8_t Enter;	//确认键
	uint8_t Up;		//上
	uint8_t Down;	//下
} Key;

void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //PB3需要禁用JTAG
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3);
	
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line1 ;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; 
	EXTI_Init(&EXTI_InitStructure);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line3 ;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	Key_Reset_All();
}

/********************************************/

int8_t Key_Enter_Get(void)	//确认键
{
	if(Key.Enter)
	{
		Key.Enter = 0;
		return 1;
	}
	return 0;
}

int8_t Key_Up_Get(void)
{
	if(Key.Up)
	{
		Key.Up = 0;
		return 1;
	}
	return 0;
}

int8_t Key_Down_Get(void)
{
	if(Key.Down)
	{
		Key.Down = 0;
		return 1;
	}
	return 0;
}

void Key_Reset_All(void)	//清除所有按键标志位
{
		Key.Enter = 0;
		Key.Up = 0;
		Key.Down = 0;
}

void EXTI3_IRQHandler(void)
{
		if (EXTI_GetITStatus(EXTI_Line3) == SET) //SW
		{
				Delay_ms(10);
			
				if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3)==Bit_RESET) //SW
						Key.Enter=1;	
				else
						Key.Enter=0;

				EXTI_ClearITPendingBit(EXTI_Line3);
		}
}

void EXTI1_IRQHandler(void)
{
		static int8_t last_flag=-1;
		uint8_t FlagStatus;
	
		if (EXTI_GetITStatus(EXTI_Line1) == SET) //UP
		{
					FlagStatus=GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1); //CLK
					
					if (last_flag!=FlagStatus)
					{
							Delay_ms(10);
						
							last_flag=GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1); //CLK
							
							if (FlagStatus==Bit_SET) //上升沿
							{
									//CLK上升沿DAT为低
									if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)==Bit_RESET ) //DAT
									{
											Key.Up = 1;
									}
									else
									{
											Key.Down = 1;
									}
							}
							else
							{
									//CLK下降沿DAT为高
									if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)==Bit_SET ) //DAT
									{
											Key.Up = 1;
									}
									else
									{
											Key.Down = 1;
									}
							}
					}

				EXTI_ClearITPendingBit(EXTI_Line1);
		}
}

